package com.example.recall_application.mvp.presenter;

import android.app.Application;

import com.blankj.utilcode.util.SPUtils;
import com.example.recall_application.app.Constants;
import com.example.recall_application.app.utils.RxUtils;
import com.example.recall_application.mvp.entity.Config;
import com.example.recall_application.mvp.entity.ConfigRequest;
import com.example.recall_application.utils.SystemUtils;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.example.recall_application.mvp.contract.ConfigContract;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/11/2019 21:41
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class ConfigPresenter extends BasePresenter<ConfigContract.Model, ConfigContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public ConfigPresenter(ConfigContract.Model model, ConfigContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void setConfigOption(int option) {
        ConfigRequest config = new ConfigRequest();
        config.setVersion(SystemUtils.packageCode(mApplication)+"");
        config.setOption(option);
        config.setUNIONID(SPUtils.getInstance().getString(Constants.uuid));
        mModel
                .setConfigOption(config)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<Config>(mErrorHandler) {
                    @Override
                    public void onNext(Config config) {
                        SPUtils.getInstance().put(Constants.Option,option);
                        if(config.getResult() == 1){
                            //mRootView.showMessage("");

                        }
                    }
                });
    }
}
